package ;

import nme.Assets;
import nme.display.Sprite;
import nme.events.Event;
import nme.Lib;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.core.proto.GeometryObject3D;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.primitives.Arrow;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Cylinder;
import org.papervision3d.objects.primitives.PaperPlane;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Sphere;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
//import ;

/**
 * ...
 * @author simple slime
 */

class Main extends Sprite 
{
	
	public function new() 
	{
		super();
		#if iphone
		Lib.current.stage.addEventListener(Event.RESIZE, init);
		#else
		addEventListener(Event.ADDED_TO_STAGE, init);
		#end
		
		var sp = new Sprite();
		var oh = new nme.ObjectHash<Dynamic, Dynamic>();
		//oh.set(sp, "hi");
		trace(oh.get(sp));
	}
	
	var view:Viewport3D;
	var scene:Scene3D;
	var camera:Camera3D;
	var render:BasicRenderEngine;
	var sphere:Sphere;
	
	private function init(e) 
	{
		// entry point
		var mat = new BitmapMaterial(Assets.getBitmapData("img/1.jpg"), true);
		mat.oneSide = true;
		
		scene = new Scene3D();
		
		sphere = new Sphere(mat, 600, 30, 30);
		scene.addChild(sphere);
		
		//var ml = new MaterialsList({
			//all: new WireframeMaterial()
		//});
		
		//var c = new Cube(ml);
		//scene.addChild(c);
		
		var arrow = new Cylinder();
		scene.addChild(arrow);
		
		var obj = new Plane();
		scene.addChild(obj);
		
		//var ml = new MaterialsList();
		//scene.addChild(new Cube(ml));
		//scene.addChild(new Arrow());
		
		view = new Viewport3D();
		camera = new Camera3D();
		render = new BasicRenderEngine();
		
		addChild(view);
		
		
		addEventListener(Event.ENTER_FRAME, onEnter);
		
		//var sp = new Sprite();
		//addChild(sp);
		//var g = sp.graphics;
		//g.beginFill(0);
		//g.drawRect(0, 0, 300, 300);
		//g.endFill();
		
		
		//render.renderScene(scene, camera, view);
	}
	
	private function onEnter(e:Event):Void 
	{
		//sphere.rotationY += 1;
		render.renderScene(scene, camera, view);
	}
	
	static public function main() 
	{
		var stage = Lib.current.stage;
		stage.scaleMode = nme.display.StageScaleMode.NO_SCALE;
		stage.align = nme.display.StageAlign.TOP_LEFT;
		
		Lib.current.addChild(new Main());
	}
	
}
